package com.qingluan.geo;

import java.util.ArrayList;
import java.util.HashMap;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.MKMapTouchListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.PoiOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.qingluan.dd.MissionSignFullscreenActivity;
import com.qingluan.dd.R;
import com.qingluan.dd.R.layout;
import com.qingluan.dd.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MapMainActivity extends Activity {
	//Baidu API argu area
	//public LocationClient qLocationClient = null;
	//public BDLocationListener qListener = new QLocationListener();
	BMapManager mBMapMan = null;	
	MapView mMapView = null; 
	MKSearch Searcher ;
	private String addr_info = null;
	private String city = null;
	private GeoPoint pointinit ;
	
	//context argu area
	private Context context_this_activity ;
	
	//widgets area
	private EditText search_edit;
	private RelativeLayout ControlWidget;
	private TextView Destinatino_info;
	//intent area 
	private Intent callBack;
	//map area
	private String destinationStr = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context_this_activity = this.getApplicationContext();
		mBMapMan = new BMapManager(context_this_activity);
		mBMapMan.init("Ei38G5wOcSolTE7LXhDf0eNj", null);
		
		setContentView(R.layout.activity_test_main);
		
		ControlWidget  = (RelativeLayout)findViewById(R.id.Map_Controler);
		search_edit = (EditText)ControlWidget.findViewById(R.id.Map_info_search);
		Destinatino_info = (TextView)ControlWidget.findViewById(R.id.Map_info_dis);
		
		mMapView=(MapView)findViewById(R.id.bmapsView);  
		mMapView.setBuiltInZoomControls(true); 
		
		Searcher = new MKSearch();

		MapController mMapController=mMapView.getController();  
		pointinit =new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));  

		
		mMapController.setCenter(pointinit);//设置地图中心点  

		mMapController.setZoom(14);//设置地
		
		mMapView.regMapTouchListner(new MKMapTouchListener(){

			@Override
			public void onMapClick(GeoPoint result) {
				// TODO Auto-generated method stub
				Searcher.reverseGeocode(result);
				mMapView.getController().animateTo(result);
				
				mMapView.getController().setZoom(13);
			}

			@Override
			public void onMapDoubleClick(GeoPoint result) {
				// TODO Auto-generated method stub
				mMapView.getController().animateTo(result);
				mMapView.getController()
						.setZoom(16);
			}

			@Override
			public void onMapLongClick(GeoPoint res) {
				// TODO Auto-generated method stub
				ArrayList<Integer> place = new ArrayList<Integer>();

				Searcher.reverseGeocode(res);
				int la = res.getLatitudeE6();
				int lon = res.getLongitudeE6();
				place.add(0, la);
				place.add(1, lon);

				callBack = new Intent(MapMainActivity.this,MissionSignFullscreenActivity.class);
				String addrStr = destinationStr;
				if (destinationStr !=null){
					callBack.putExtra("placeStr", addrStr);
					callBack.putExtra("place", place);
					setResult(RESULT_OK,callBack);
					MapMainActivity.this.finish();
				}
				
				
			}
			
		});
		Searcher.init(mBMapMan, new MKSearchListener(){

			@Override
			public void onGetAddrResult(MKAddrInfo result, int error) {
				// TODO Auto-generated method stub
				if (error != 0){
					 String str = String.format("错误号：%d", error);  
					 Toast.makeText(context_this_activity, str, Toast.LENGTH_SHORT).show();
					 return ;
				}

				GeoPoint point = result.geoPt;
				pointinit = point;
				
				mMapView.getController().animateTo(point);
				if (result.type == MKAddrInfo.MK_GEOCODE){
					int lan =  result.geoPt.getLatitudeE6();
					int lon =  result.geoPt.getLongitudeE6();
					mMapView.getController().animateTo(new  GeoPoint(lan,lon));
					mMapView.getController().setZoom(16);
				}
				  
				if (result.type == MKAddrInfo.MK_REVERSEGEOCODE){
					String strInfo = result.strAddr;  
					
					destinationStr = strInfo;
					Destinatino_info.setText(strInfo);
				}
			}

			@Override
			public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onGetDrivingRouteResult(MKDrivingRouteResult arg0,
					int arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onGetPoiDetailSearchResult(int arg0, int arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onGetPoiResult(MKPoiResult res, int type, int error) {
				// TODO Auto-generated method stub
				if ( error == MKEvent.ERROR_RESULT_NOT_FOUND){  
					Toast.makeText(MapMainActivity.this, "抱歉，未找到结果",Toast.LENGTH_LONG).show();  
					return ;  
				}else if (error != 0 || res == null) {  
					Toast.makeText(MapMainActivity.this, "搜索出错啦..", Toast.LENGTH_LONG).show();  
					return;  
				}  
				
				PoiOverlay poiOverlay = new PoiOverlay(MapMainActivity.this, mMapView);  
				poiOverlay.setData(res.getAllPoi());  
				mMapView.getOverlays().clear();  
				mMapView.getOverlays().add(poiOverlay); 
				mMapView.refresh();  
				//当ePoiType为2（公交线路）或4（地铁线路）时， poi坐标为空  
				for(MKPoiInfo info : res.getAllPoi() ){  
					if ( info.pt != null ){  
						mMapView.getController().animateTo(info.pt);  
						break;  
					}  
				}  
			}

			@Override
			public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1,
					int arg2) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onGetTransitRouteResult(MKTransitRouteResult arg0,
					int arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onGetWalkingRouteResult(MKWalkingRouteResult arg0,
					int arg1) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		

	}
	public void search_sure_button(View v){
		addr_info = search_edit.getText().toString();
		Searcher.poiSearchNearBy(addr_info, pointinit, 10000);
		Destinatino_info.setText(addr_info+"附近...");
		Toast.makeText(context_this_activity, addr_info, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test_main, menu);
		return true;
	}
	/*
	@Override
	protected void onDestroy(){
	        mMapView.destroy();
	        if(mBMapMan!=null){
	                mBMapMan.destroy();
	                mBMapMan=null;
	        }
	        super.onDestroy();
	}
	*/
	@Override
	protected void onPause(){
	        mMapView.onPause();
	        if(mBMapMan!=null){
	               mBMapMan.stop();
	        }
	        super.onPause();
	}
	@Override
	protected void onResume(){
	        mMapView.onResume();
	        if(mBMapMan!=null){
	                mBMapMan.start();
	        }
	        
	       super.onResume();
	}

	

	
}
